package com.lzy.cloud.search.client.service.impl;

import com.lzy.cloud.search.client.entity.EsQuery;
import com.lzy.cloud.search.client.service.CompoundQueriesService;
import com.lzy.cloud.search.client.util.EsClient;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * 复合查询
 */
@Service
public class CompoundQueriesServiceImpl implements CompoundQueriesService {

    @Autowired
    private EsClient client;

    @Override
    public SearchResponse constantScoreQuerys(EsQuery esQuery)   {

        QueryBuilder qb = constantScoreQuery(
                termQuery(
                        esQuery.getFieldName(),
                        esQuery.getFieldText()
                )      //查询语句
        ).boost(2.0f);   //分数
        SearchResponse response = client.getConnection().prepareSearch(esQuery.getIndices())//可以是多个index
                .setTypes(esQuery.getTypes())//可以是多个类型
                .setQuery(qb)    // Query 查询条件
                .get();

        //        QueryBuilder qb = constantScoreQuery(
//                termQuery(
//                        "user",
//                        "kimchy"
//                )      //查询语句
//
//        ).boost(2.0f);   //分数
//
//        SearchResponse response = client.getConnection().prepareSearch("twitter")//可以是多个index
//                .setTypes("tweet")//可以是多个类型
//                .setQuery(qb)    // Query 查询条件
//                .get();

        return response;
    }

}
